package com.example.mybooksapp;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;
import android.widget.EditText;
import android.widget.Button;
import com.example.mybooksapp.Book; // Book 类的包路径（需和实际项目包一致）
import com.example.mybooksapp.BookAdapter; // BookAdapter 类的包路径（需和实际项目包一致）
public class BookListActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText etBookId;
    private EditText etBookName;
    private Button btnAdd;
    private Button btnDelete;
    private Button btnUpdate;
    private Button btnQuery;
    private RecyclerView rvBooks;
    private BookAdapter bookAdapter;
    private List<Book> bookList;
    // 模拟当前登录账号（实际项目中从SharedPreferences或服务器获取）
    private String currentUserEmail = "test@example.com";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_list);

        // 初始化View
        initView();
        // 初始化数据
        initData();
        // 设置适配器
        initAdapter();
        // 设置按钮点击监听
        setListeners();
    }

    private void initView() {
        etBookId = findViewById(R.id.et_book_id);
        etBookName = findViewById(R.id.et_book_name);
        btnAdd = findViewById(R.id.btn_add);
        btnDelete = findViewById(R.id.btn_delete);
        btnUpdate = findViewById(R.id.btn_update);
        btnQuery = findViewById(R.id.btn_query);
        rvBooks = findViewById(R.id.rv_books);
    }

    private void initData() {
        bookList = new ArrayList<>();
        // 模拟空查询显示全部书籍（实际项目中从数据库/服务器获取）
    }

    private void initAdapter() {
        bookAdapter = new BookAdapter(this, bookList);
        rvBooks.setLayoutManager(new LinearLayoutManager(this));
        rvBooks.setAdapter(bookAdapter);
    }

    private void setListeners() {
        btnAdd.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
        btnQuery.setOnClickListener(this);
    }
    private void clearInput() {
        etBookId.setText("");   // etBookId 是输入书号的 EditText 控件
        etBookName.setText(""); // etBookName 是输入书名的 EditText 控件
    }
    private boolean isInputValid(String bookId, String bookName) {
        if (bookId.isEmpty() || bookName.isEmpty()) {
            Toast.makeText(this, "书号和书名不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        // 可添加其他校验逻辑（如书号格式等）
        return true;
    }

    @Override
    public void onClick(View v) {
        String bookId = etBookId.getText().toString().trim();
        String bookName = etBookName.getText().toString().trim();

        int viewId = v.getId();
        if (viewId == R.id.btn_add) {
            if (isInputValid(bookId, bookName)) {
                Book newBook = new Book(bookId, bookName, currentUserEmail);
                bookList.add(newBook);
                bookAdapter.notifyDataSetChanged();
                Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                clearInput();
            }
        } else if (viewId == R.id.btn_delete) {
            if (isInputValid(bookId, bookName)) {
                // 实际项目中根据书号查找并删除
                boolean isDeleted = false;
                for (int i = 0; i < bookList.size(); i++) {
                    if (bookList.get(i).getBookId().equals(bookId) && bookList.get(i).getUserEmail().equals(currentUserEmail)) {
                        bookList.remove(i);
                        isDeleted = true;
                        break;
                    }
                }
                if (isDeleted) {
                    bookAdapter.notifyDataSetChanged();
                    Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                    clearInput();
                } else {
                    Toast.makeText(this, "未找到对应书籍", Toast.LENGTH_SHORT).show();
                }
            }
        } else if (viewId == R.id.btn_update) {
            if (isInputValid(bookId, bookName)) {
                // 实际项目中根据书号查找并修改
                boolean isUpdated = false;
                for (Book book : bookList) {
                    if (book.getBookId().equals(bookId) && book.getUserEmail().equals(currentUserEmail)) {
                        book.setBookName(bookName);
                        isUpdated = true;
                        break;
                    }
                }
                if (isUpdated) {
                    bookAdapter.notifyDataSetChanged();
                    Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                    clearInput();
                } else {
                    Toast.makeText(this, "未找到对应书籍", Toast.LENGTH_SHORT).show();
                }
            }
        } else if (viewId == R.id.btn_query) {
            if (bookId.isEmpty() && bookName.isEmpty()) {
                // 空查询，显示当前账号的全部书籍
                bookAdapter.notifyDataSetChanged();
                Toast.makeText(this, "显示全部书籍", Toast.LENGTH_SHORT).show();
            } else {
                // 按条件查询（实际项目中根据输入条件过滤）
                List<Book> queryResult = new ArrayList<>();
                for (Book book : bookList) {
                    if (book.getUserEmail().equals(currentUserEmail) &&
                            (book.getBookId().contains(bookId) || book.getBookName().contains(bookName))) {
                        queryResult.add(book);
                    }
                }
                bookAdapter = new BookAdapter(this, queryResult);
                rvBooks.setAdapter(bookAdapter);
                Toast.makeText(this, "查询结果：" + queryResult.size() + "条", Toast.LENGTH_SHORT).show();
            }
        }
    }
}